package com.fone.player.online.adapter;

import java.util.List;

import com.fone.player.R;
import com.fone.player.entity.LiveInfo;
import com.fone.player.util.FoneUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LiveAdapter extends BaseAdapter {

	/** 日志标签 */
	// private static final String TAG = "LiveAdapter";

	private String shost;
	private List<LiveInfo> liveList;
	private LayoutInflater layoutInflater;

	DisplayImageOptions options1, options2;

	public LiveAdapter(Context context, String shost, List<LiveInfo> liveList) {
		super();

		options1 = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_image_bg).showImageForEmptyUri(R.drawable.default_image_bg).showImageOnFail(R.drawable.default_image_bg).cacheInMemory(true).cacheOnDisk(true)
		// .displayer(new RoundedBitmapDisplayer(20))
				.build();

		options2 = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_340_192).showImageForEmptyUri(R.drawable.default_340_192).showImageOnFail(R.drawable.default_340_192).cacheInMemory(true).cacheOnDisk(true).build();

		initData(shost, liveList);
		layoutInflater = LayoutInflater.from(context);
	}

	public void initData(String shost, List<LiveInfo> liveList) {
		this.shost = shost;
		this.liveList = liveList;
	}

	@Override
	public int getCount() {
		return liveList.size();
	}

	@Override
	public Object getItem(int position) {
		return liveList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LiveInfo info = liveList.get(position);

		ViewHolder vHolder = null;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_live, null);
			// 创建 ViewHodler 对象
			vHolder = new ViewHolder();
			vHolder.tv = (ImageView) convertView.findViewById(R.live.pic_tv);
			vHolder.content = (ImageView) convertView.findViewById(R.live.pic_content);
			vHolder.title = (TextView) convertView.findViewById(R.live.text_name);
			vHolder.play = (TextView) convertView.findViewById(R.live.text_playing);
			vHolder.next = (TextView) convertView.findViewById(R.live.text_next);

			// 设置 Tag
			convertView.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}

		// 设置图片 pic2：截图 pic1：台标
		// ImageDownloadModule.getInstance().download(FoneUtil.getAbsoluteUrl("",
		// shost, info.pic1), R.drawable.default_184, vHolder.tv);
		if (TextUtils.isEmpty(info.pic1)) {

			Log.i("LiveAdapter", info.name + " no tv logo");
			vHolder.tv.setVisibility(View.GONE);
		} else {
			vHolder.tv.setVisibility(View.VISIBLE);
			Log.i("LiveAdapter", info.name + " tv logo is " + info.pic1);
			ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic1), vHolder.tv, options1);
		}

		// L.v(TAG, "getView", url);

		// ImageDownloadModule.getInstance().download(FoneUtil.getAbsoluteUrl("",
		// shost, info.pic2), R.drawable.default_image_bg, vHolder.content);
		ImageLoader.getInstance().displayImage(FoneUtil.getAbsoluteUrl("", shost, info.pic2), vHolder.content, options2);

		if (info.exts != null && info.exts.ext != null && info.exts.ext.cntList != null && info.exts.ext.cntList.size() > 0) {
			if (info.exts.ext.cntList.size() > 1) {
				vHolder.next.setText(info.exts.ext.cntList.get(1).time + " " + info.exts.ext.cntList.get(1).name);
			} else {
				vHolder.next.setText("");
			}
			vHolder.play.setText("正在播放：" + info.exts.ext.cntList.get(0).name);
		} else {
			vHolder.play.setText("");
			vHolder.next.setText("");
		}
		vHolder.title.setText(getName(info.name, info.hd));
		return convertView;
	}

	static class ViewHolder {
		ImageView tv;
		ImageView content;
		TextView title;
		TextView play;
		TextView next;
	}

	/**
	 * 是否高清处理
	 * 
	 * @param name
	 *            视频名称
	 * @param hd
	 *            视频是否高清 0：高清 1：非高清
	 * @return 参数X描述... （注意：请将参数、描述都对齐）
	 */
	private String getName(String name, String hd) {
		if (TextUtils.isEmpty(hd)) {
			return name;
		} else if (hd.equals("0")) {
			return name + "【高清】";
		} else {
			return name;
		}
	}
}
